package com.golden.thumbnail.handler.impl;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import com.golden.thumbnail.handler.IHandler;

import net.coobird.thumbnailator.Thumbnails;

public class ThumbnailHandler implements IHandler {

	public boolean processor(String inputPath, String outPath, String filename, List<String> scaleList) {
		String fullFileInfo = inputPath + filename;
		filename = filename.substring(0,filename.lastIndexOf("."));
		for(String scales : scaleList) {
			String[] values = scales.toUpperCase().split("X");
			String thumbnailName = filename + "_" + values[0] + "X" + values[1] + ".jpg";
			File source = new File(fullFileInfo);
			if(!source.exists()) {
				System.out.println(source);
				return false;
			}
			try {
				Thumbnails.of(fullFileInfo).size(Integer.valueOf(values[0]), Integer.valueOf(values[1]))
				.toFile(outPath + thumbnailName);
			} catch (Exception e) {
				e.printStackTrace();
				return false;
			}
		}
		return true;
	}
	
	
	public static void main(String[] args) {
		IHandler handler = new ThumbnailHandler();
		List<String> scaleList = new ArrayList<String>();
		scaleList.add("150x300");
		scaleList.add("300x600");
		scaleList.add("400x800");
		System.out.println(handler.processor("d:/", "d:/", "800.jpg", scaleList));
	}

}
